#include <stdio.h>
#include <stdlib.h>
#define DataType int
//--------Define Sequence Stack------------------------------------------------//
struct SeqStack{
       int MAXNUM;
       int t;
       DataType *s;
       };
typedef struct SeqStack *PSeqStack;

//--------Define Creating a Sequence Stack-------------------------------------//
PSeqStack createEmptyStack_seq(int m){
          PSeqStack pastack=(PSeqStack)malloc(sizeof(struct SeqStack));
          if (pastack!=NULL){
                            pastack->s=(DataType *)malloc(sizeof(DataType) *m);
                            if (pastack->s){
                                           pastack->MAXNUM=m;
                                           pastack->t=-1;
                                           return pastack;
                                           }
                            else free (pastack);
                            }
          printf("Out of space!!\n");
          return NULL;
          }
//--------Define Checking whether a Sequence Stack is empty--------------------//
int isEmptyStack_seq(PSeqStack pastack){
    int stack_top=pastack->t;
    if (stack_top<0){
                     return 1;
                     }
    else return 0;
    }
//--------Define Pushing an element ("x") into a Sequence Stack ("pastack")----//
void push_seq(PSeqStack pastack, DataType x){
     if (pastack->t>=pastack->MAXNUM-1)
        printf("Overflow, pushing failed!\n");
     else{
          pastack->t=pastack->t+1;
          pastack->s[pastack->t]=x;
          }
     }
//--------Define Poping an element out from a Sequence Stack ("pastack")-------//
void pop_seq(PSeqStack pastack){
     if (pastack->t<0)
        printf("Underflow, poping failed!\n");
     else{
          pastack->t=pastack->t-1;
          }
     }
//--------Define getting Top element out from a Sequence Stack ("pastack")-----//     
DataType top_seq(PSeqStack pastack){
     if (pastack->t<0)
        printf("This stack is empty!\n");
     else{
          return pastack->s[pastack->t];
          }
     }

//--------A factorial calculator-----------------------------------------------//  
int nfact (int n){
    int res;
    PSeqStack st;
    st=createEmptyStack_seq(n);
    while (n>0){
          push_seq(st,n);
          n=n-1;
          }
    res=1;
    while (!isEmptyStack_seq(st)){
          res=res*top_seq(st);
          pop_seq(st);
          }
    free(st);
    return res;
    }
//--------Main Function--------------------------------------------------------//  
int main(void)
{
  int n=5;
  int fact_n=nfact(n);
  printf("%d", fact_n);
  system("PAUSE");	
  return 0;
}
